#include "playscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QTimer>
#include<QLabel>
#include<QDebug>
#include<QFont>
#include<QSoundEffect>
#include<QPropertyAnimation>
#include"mypushbutton.h"
#include"chooselevelscene.h"
#include"mycoin.h"
#include"dataconfig.h"
// PlayScene::PlayScene(QWidget *parent)
//     : QMainWindow{parent}
// {}

PlayScene::PlayScene(int levelNum)
{
    this->levelIndex=levelNum;
    this->setFixedSize(320,588);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));
    this->setWindowTitle("游戏界面");
    //创建菜单栏
    QMenuBar *bar=new QMenuBar();
    setMenuBar(bar);
    QMenu *startMenu=bar->addMenu("开始");
    QAction *quitAction=startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //创建音效
    QSoundEffect *closeSound=new QSoundEffect();
    QSoundEffect *flipSound=new QSoundEffect();
    QSoundEffect *winSound=new QSoundEffect();
    //创建关闭按钮
    MyPushButton *closeBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
    //创建按钮监听
    connect(closeBtn,&MyPushButton::clicked,[=](){
        closeSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));
        closeSound->play();
        QTimer::singleShot(500,this,[=](){
            emit this->playCloseScene();
        });
    });
    //显示当前关卡
    QLabel *label=new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("宋体");
    font.setPointSize(15);
    font.setBold(true);
    QString str1=QString("关卡:%1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(str1);
    label->setGeometry(30,this->height()-50,120,50);

    //初始化
    dataConfig config;
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            this->gameArray[i][j]=config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图片显示
    QLabel *winLabel=new QLabel();
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());
    //显示关卡背景图
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            QLabel *label1=new QLabel;
            label1->setGeometry(0,0,50,50);
            label1->setPixmap(QPixmap(":/res/BoardNode.png"));
            label1->setParent(this);
            label1->move(57+i*50,200+j*50);
            //创建金币
            QString str;
            if(this->gameArray[i][j]==1)
            {
                str=":/res/Coin0001.png";
            }
            else
            {
                str=":/res/Coin0008.png";
            }
            MyCoin *mycoin=new MyCoin(str);
            mycoin->setParent(this);
            mycoin->move(59+i*50,204+j*50);
            mycoin->posX=i;
            mycoin->posY=j;
            mycoin->flag=this->gameArray[i][j];
            //方便后期维护
            coinBtn[i][j]=mycoin;
            connect(mycoin,&MyCoin::clicked,[=](){
                flipSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
                flipSound->play();
                mycoin->changerFlag();
                this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
                //延迟翻转
                QTimer::singleShot(300,this,[=](){
                    if(mycoin->posX+1<=3)
                        {
                        coinBtn[mycoin->posX+1][mycoin->posY]->changerFlag();
                        this->gameArray[mycoin->posX+1][mycoin->posY]=this->gameArray[mycoin->posX+1][mycoin->posY]==0?1:0;
                    }
                    if(mycoin->posX-1>=0)
                        {
                        coinBtn[mycoin->posX-1][mycoin->posY]->changerFlag();
                        this->gameArray[mycoin->posX-1][mycoin->posY]=this->gameArray[mycoin->posX+1][mycoin->posY]==0?1:0;
                    }
                    if(mycoin->posY+1<=3)
                    {
                        coinBtn[mycoin->posX][mycoin->posY+1]->changerFlag();
                        this->gameArray[mycoin->posX][mycoin->posY+1]=this->gameArray[mycoin->posX][mycoin->posY+1]==0?1:0;
                    }
                    if(mycoin->posY-1>=0)
                    {
                        coinBtn[mycoin->posX][mycoin->posY-1]->changerFlag();
                        this->gameArray[mycoin->posX][mycoin->posY-1]=this->gameArray[mycoin->posX][mycoin->posY-1]==0?1:0;
                    }
                    this->isWin=true;
                    for(int i=0;i<4;i++)
                    {
                        for(int j=0;j<4;j++)
                        {
                            if(coinBtn[i][j]->flag==false)
                            {
                                this->isWin=false;
                                break;
                            }
                        }
                    }
                    if(this->isWin==true)
                        {
                        winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));
                        winSound->play();
                        qDebug()<<"胜利了";
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                coinBtn[i][j]->isWin=true;
                            }
                        }
                        //移动胜利图片
                        QPropertyAnimation *animation=new QPropertyAnimation(winLabel,"geometry");
                        //设置时间间隔
                        animation->setDuration(1000);
                        //设置开始位置
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        //设置结束位置
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                        //设置动画曲线
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start();
                    }
                });
            });

        }
    }
}
void PlayScene ::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/background3.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //画图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}
